package com.yjhb.android.sbz.mvp.managertasklist;

import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.AddTask;
import com.yjhb.android.sbz.entity.ManagerTask;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.manager.ResManager;
import com.yjhb.android.sbz.mvp.ConventionPresenter;

import java.math.BigDecimal;
import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2020/12/11 17:16
 */
public class ManagerTaskListPresenter extends ConventionPresenter<ManagerTaskListView> {
    private final String TAG = "ManagerTaskListPresenter";

    @Override
    protected void onDestroy() {

    }

    public ManagerTaskListPresenter(ManagerTaskListView managerTaskListView) {
        super(managerTaskListView);
    }

    public void getTaskList(Integer state, Integer size, final Integer current) {
        HttpTool.request(model.getManagerTaskList(state, null, size, current), new HttpObserver<List<ManagerTask>>() {
            @Override
            public void onResult(List<ManagerTask> data) {
                if (view != null) {
                    if (current == 1) {
                        view.onRefreshData(data);
                    } else {
                        view.onLoadData(data);
                    }
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    public void restartTask(String taskId) {
        if (view != null) {
            view.onWait("恢复中");
        }
        HttpTool.request(model.stopOrRestart(taskId), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess("已恢复", taskId);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    /**
     * 任务详情
     */
    public void getDetails(final ManagerTask managerTask) {
        if (view != null) {
            view.onWait(ResManager.getString(R.string.task_tips_loading_data));
        }
        HttpTool.request(model.taskDetail(managerTask.getTask().getId() + ""), new HttpObserver<AddTask>() {
            @Override
            public void onResult(AddTask data) {
                if (view != null) {
                    view.onTaskDetail(data, managerTask);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    /**
     * 首页推荐
     *
     * @param taskId
     * @param time   推荐时长 /小时
     */
    public void showMainPage(String taskId, int time) {
        if (view != null) {
            view.onWait(ResManager.getString(R.string.task_tips_topping));
        }
        HttpTool.request(model.showHomePage(taskId, time), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess(ResManager.getString(R.string.task_tips_topping_success), taskId);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    /**
     * 刷新任务
     *
     * @param taskId
     */
    public void refreshTask(String taskId) {
        if (view != null) {
            view.onWait(ResManager.getString(R.string.task_tips_refresh_task));
        }
        HttpTool.request(model.flushTask(taskId), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess(ResManager.getString(R.string.task_tips_refresh_task_success), taskId);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    /**
     * 添加任务名额
     *
     * @param taskId
     * @param count
     */
    public void addTaskCount(String taskId, int count) {
        if (view != null) {
            view.onWait(ResManager.getString(R.string.task_tips_checking));
        }
        HttpTool.request(model.addTaskStock(taskId, count), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess(ResManager.getString(R.string.task_tips_add_stock_success), taskId);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    /**
     * 加钱
     *
     * @param taskId
     * @param money
     */
    public void addMoney(String taskId, BigDecimal money) {
        if (view != null) {
            view.onWait(ResManager.getString(R.string.task_tips_checking));
        }
        HttpTool.request(model.addTaskMoney(taskId, money), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess(ResManager.getString(R.string.task_tips_add_money_success), taskId);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    /**
     * 开启15分钟快审
     *
     * @param taskId
     */
    public void fastCheck(String taskId, boolean isOpen) {
        if (view != null) {
            if (isOpen)
                view.onWait(ResManager.getString(R.string.task_tips_fast_checking));
            else
                view.onWait(ResManager.getString(R.string.task_tips_fast_colseing));
        }
        HttpTool.request(model.fastCheck(taskId), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    if (isOpen)
                        view.onSuccess(ResManager.getString(R.string.task_tips_fast_check_success), taskId);
                    else
                        view.onSuccess(ResManager.getString(R.string.task_tips_fast_colse_success), taskId);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    /**
     * 暂停或重启任务
     *
     * @param taskId
     */
    public void stopOrRestartTask(String taskId, final boolean isStop) {
        if (view != null) {
            view.onWait(isStop ? ResManager.getString(R.string.task_tips_stopping) :
                    ResManager.getString(R.string.task_tips_restarting));
        }
        HttpTool.request(model.stopOrRestart(taskId), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess(isStop ? ResManager.getString(R.string.task_tips_stop_success) :
                            ResManager.getString(R.string.task_tips_restart_success), taskId);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    /**
     * 申请退款
     */
    public void downTask(Integer taskId) {
        if (view != null) {
            view.onWait("正在申请...");
        }
        HttpTool.request(model.downTask(taskId), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess("退款成功！", "" + taskId);
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

    public void loadTask(Integer taskId) {
        HttpTool.request(model.getManagerTaskList(null, taskId, null, null), new HttpObserver<List<ManagerTask>>() {
            @Override
            public void onResult(List<ManagerTask> data) {
                if (data != null && data.size() > 0) {
                    if (view != null) {
                        view.onTask(data.get(0));
                    }
                } else {
                    if (view != null)
                        view.onFailure(-2, ResManager.getString(R.string.task_tips_err_not_task));
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    public void deleteTask(Integer taskId) {
        if (view != null) {
            view.onWait("处理中...");
        }
        if (model != null) {
            HttpTool.request(model.deleteDown(taskId), new HttpObserver<Object>() {
                @Override
                public void onResult(Object data) {
                    if (view != null) {
                        view.onTaskDeleteSuccess();
                    }
                }

                @Override
                public void onError(int code, String msg) {
                    if (view != null) {
                        view.onFailure(code, msg);
                    }
                }
            });
        }
    }

}
